package top.lyc829.aircontroller.ui.index

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import kotlinx.android.synthetic.main.activity_main.*
import top.lyc829.aircontroller.R
import top.lyc829.aircontroller.ui.chart.ChartActivity
import top.lyc829.aircontroller.ui.login.LoginActivity
import top.lyc829.aircontroller.ui.searchVideo.SearchVideoActivity


class MainActivity : AppCompatActivity() {

    lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

        // 判断是否登录
        viewModel.isLogin.observe(this, {
            when (it) {
                true -> viewModel.refreshTemAndHum()
                else -> {
                    val intent = Intent(this, LoginActivity::class.java)
                    startActivityForResult(intent, 1)
                }
            }
        })

        viewModel.airInfo.observe(this, {
            //改变视图
            when (it.state) {
                "off" -> {
                    t_on_off.text = "OFF"
                    t_mode.text = it.mode
                    t_mode.visibility = View.INVISIBLE
                }
                "on" -> {
                    t_mode.text = it.mode
                    t_mode.visibility = View.VISIBLE
                    t_on_off.text = it.tem
                }
            }
        })

        viewModel.temAndHum.observe(this, {
            t_tem.text = "温度:${it.temperature}℃"
            t_hum.text = "湿度:${it.humidity}%"

        })

    }

    override fun onResume() {
        super.onResume()
        Log.d("MyTag", "Resume")

        viewModel.updateLoginState()

        btn_mode.setOnClickListener {
            viewModel.changeMode()
        }

        btn_on_off.setOnClickListener {
            viewModel.changeOnOff()
        }

        btn_add.setOnClickListener {
            viewModel.addTem()
        }

        btn_reduce.setOnClickListener {
            viewModel.reduceTem()
        }

//        btn_go_video.setOnClickListener {
//            val intent = Intent(this, SearchVideoActivity::class.java)
//            startActivity(intent)
//        }
//
//        btn_go_chart.setOnClickListener {
//            //TODO: 点击按钮跳转到图表页面
//            val intent = Intent(this, ChartActivity::class.java)
//            startActivity(intent)
//        }

    }

    /*
     * 加载选项菜单
     */
    @SuppressLint("ResourceType")
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.layout.menu_main,menu);
        return true
    }

    /*
     * 选项菜单点击事件
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.chart_item){
            //TODO: 点击按钮跳转到图表页面
            val intent = Intent(this, ChartActivity::class.java)
            startActivity(intent)
        }else if (item.itemId == R.id.video_item){
            //TODO: 点击按钮跳转到视频页面
            val intent = Intent(this, SearchVideoActivity::class.java)
            startActivity(intent)
        }
        return super.onOptionsItemSelected(item)
    }
}